//
//  ActivityListViewController.m
//  DoubanDemo1
//
//  Created by lanou3g on 15/11/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityListView.h"
#import "ActivityTableViewCell.h"
#import "ActivityDetailViewController.h"
#import "ActivityListHandle.h"
#import "DataTool.h"
#import "ActivityList.h"
#import "ActivityDetail.h"


@interface ActivityListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)ActivityListView *activityListView;



@end

@implementation ActivityListViewController

- (void)loadView
{
    self.activityListView = [[ActivityListView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.activityListView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"活动";
    
    self.activityListView.tableView.delegate = self;
    self.activityListView.tableView.dataSource = self;
    
    __weak typeof(self) weakSelf = self;
    [[ActivityListHandle shareInstence]getActivityListDataSourceWithGoBackBlock:^{
        [weakSelf.activityListView.tableView reloadData];
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ActivityListHandle shareInstence].activityListArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_id = @"cell_id";
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    if (!cell) {
        cell = [[ActivityTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
    }
    
    ActivityList *activityList = [[ActivityList alloc]init];
    activityList = [ActivityListHandle shareInstence].activityListArray[indexPath.row];
    cell.titleLabel.text = activityList.title;
    
    
    NSString *timeStr = [NSString stringWithFormat:@"%@ %@",activityList.begin_time,activityList.end_time];
    
    cell.topILView.rightLabel.text = timeStr;
    cell.topILView.rightLabel.font = [UIFont fontWithName:nil size:10];
    cell.topILView.rightLabel.adjustsFontSizeToFitWidth = YES;
    //self.topILView.rightLabel.numberOfLines = 0;
    
    cell.middleILView.rightLabel.text = activityList.address;
    cell.middleILView.rightLabel.font = [UIFont fontWithName:nil size:13];
    
    NSString *typeStr = [NSString stringWithFormat:@"类型： %@",activityList.category_name];
    cell.downILView.rightLabel.text = typeStr;
    
    cell.downILView.rightLabel.font = [UIFont fontWithName:nil size:13];
    
    cell.wisherCountLabel1.text = @"感兴趣";
    cell.wisherCountLabel1.font = [UIFont fontWithName:nil size:13];
    
    NSString *wisher_countStr = [NSString stringWithFormat:@"%ld",activityList.wisher_count];
    cell.wisherCountLabel2.font = [UIFont fontWithName:nil size:13];
    cell.wisherCountLabel2.text = wisher_countStr;
    
    cell.participantCountLabel1.text = @"参加";
    cell.participantCountLabel1.font = [UIFont fontWithName:nil size:13];
    NSString *participant_countStr = [NSString stringWithFormat:@"%ld",activityList.participant_count];
    cell.participantCountLabel2.font = [UIFont fontWithName:nil size:13];
    cell.participantCountLabel2.text = participant_countStr;
    
    if (activityList.myImage == nil && activityList.isLoading == NO) {
        //需要重新加载
        [activityList loadImage];
        //检测activityList.myImage的值得改变
        //1. 给activityList添加一个观察者
        [activityList addObserver:self forKeyPath:@"myImage" options:NSKeyValueObservingOptionNew context:(__bridge void *)(indexPath)];
    } else {
        //加载完成 不需要再次加载 直接赋值
        cell.activityImageView.image = activityList.myImage;
    }
    
    
    return cell;
}

//2 实现KVO方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //枚举值 NSKeyValueChangeNewKey枚举出来change对应的key
    UIImage *newImage = change[NSKeyValueChangeNewKey];
    //安全操作 判断是否请求成功 未成功就不赋值
    if (newImage == nil) {
        return;
    }
    //转化成NSIndexPath类型进行操作
    NSIndexPath *indexPath = (__bridge NSIndexPath *)(context);
    //获取到当前的cell
    //判断indexPath是不是当前现实中的一个
    NSArray *array = [self.activityListView.tableView indexPathsForVisibleRows];
    
    if ([array containsObject:indexPath]) {
        //获取到当前要刷新的cell
        ActivityTableViewCell *cell = (ActivityTableViewCell *)[self.activityListView.tableView cellForRowAtIndexPath:indexPath];
        //替换cell的imgView图片
        cell.activityImageView.image = newImage;
        [self.activityListView.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    //4.移除观察者
    [object removeObserver:self forKeyPath:@"myImage"];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - UITableViewDelegate
//实现协议方法 点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    ActivityDetailViewController *activityVC = [[ActivityDetailViewController alloc]init];
    
    ActivityList *activityList = [[ActivityListHandle shareInstence] activityListForIndexPath:indexPath];
    
    activityVC.activityList = activityList;
    
    NSLog(@"%@",activityVC.activityList);
    NSLog(@"%@",activityList);
    [self.navigationController pushViewController:activityVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
